package com.wsh.crawler.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling
public class SchedulingConfig implements SchedulingConfigurer {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingConfig.class);

    @Override
    public void configureTasks(ScheduledTaskRegistrar registrar) {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(4); // 4线程，避免一个线程死掉所有任务都停
        scheduler.setThreadNamePrefix("scheduler-");
        scheduler.setErrorHandler(t -> LOGGER.error("定时任务未捕捉到的错误：", t));
        scheduler.setRemoveOnCancelPolicy(true);
        scheduler.initialize();
        registrar.setTaskScheduler(scheduler);
    }
}